package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.View
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.newtv.TvUserInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.GlideUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.util.jump
import com.ellricky.newtv.adapter.TYPE_PREPARE
import com.ellricky.newtv.bean.TvCourseEndDate
import com.ellricky.newtv.dialog.VipHitFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private var tvUserInfo: TvUserInfo? = null

    override fun getContentLayout(): Int {
        return R.layout.activity_main
    }

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun initUIView() {
        tvUserInfo = LocalCacheUtil.readTvUserInfo()
        rl_login.setOnClickListener(this)
        rl_connect.setOnClickListener(this)
        rl_course.setOnClickListener(this)
        rl_prepare.setOnClickListener(this)
        rl_class_manage.setOnClickListener(this)
        refreshUi()
        loadData()
        LocalCacheUtil.saveTvBindClass(null)
    }

    private fun refreshUi() {
        if (tvUserInfo != null) {
            GlideUtil.into(this, tvUserInfo?.E_Head, iv_avatar)
            tv_name.text = tvUserInfo?.U_Name
            tv_name.visibility = View.VISIBLE
            iv_avatar.visibility = View.VISIBLE
            tv_class_status.text = "当前班级${tvUserInfo?.class_num}个"
            rl_login.setBackgroundResource(R.drawable.ic_icon_login)
        } else {
            tv_name.visibility = View.GONE
            iv_avatar.visibility = View.GONE
            tv_class_status.text = "未登录"
            rl_login.setBackgroundResource(R.drawable.ic_icon_un_login)
        }
    }

    override fun onResume() {
        super.onResume()
        tvUserInfo = LocalCacheUtil.readTvUserInfo()
        refreshUi()
    }

    private fun loadData() {
        if (LocalCacheUtil.isTvLogin()) {
            val url = EAppUtil.getTvApiHost()
            val paramMap = mutableMapOf(
                "action" to "userDate",
                "eid" to LocalCacheUtil.getTvUid()
            )
            HttpUtils.getInstance()
                .postFrom(url, paramMap, object : NewEntityCallback<TvCourseEndDate>(this, false) {
                    override fun onSuccess(endDate: TvCourseEndDate, var2: String?) {
                        if (endDate.endDay <= 30) {
                            val fragment = VipHitFragment(endDate.endDay)
                            fragment.show(this@MainActivity.supportFragmentManager, "vip")
                            fragment.setOnVipDialogListener(object :
                                VipHitFragment.OnVipDialogListener {
                                override fun onContact() {
                                    jump<NewTvConnectActivity>()
                                }
                            })
                        }
                    }
                }, this)
        }
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.rl_login -> {
                if (!LocalCacheUtil.isTvLogin()) {
                    jump<NewTvLoginActivity>()
                } else {
                    jump<NewTvUserInfoActivity>()
                }
            }
            R.id.rl_connect -> {
                jump<NewTvConnectActivity>()
            }
            R.id.rl_course -> {
                jump<NewTvCourseTypeActivity>()
            }
            R.id.rl_prepare -> {
                jump<NewTvPreLessonActivity> {
                    putExtra(BaseConstant.KEY_FROM, TYPE_PREPARE)
                }
            }
            R.id.rl_class_manage -> {
                jump<NewTvClassManageActivity>()
            }
        }
    }
}